package com.xc.ezplan.admin.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.xc.ezplan.admin.entity.AdminJob;
import com.xc.ezplan.admin.service.AdminJobService;
import com.xc.ezplan.admin.vo.AdminJobVo;
import com.xc.ezplan.core.base.BaseSearchBody;
import com.xc.ezplan.core.base.EzResult;
import com.xc.ezplan.core.exception.BadRequestException;
import com.xc.ezplan.log.annotation.Log;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Set;

/**
 * @author lixingchen
 * @date 2022/11/19
 * @description 岗位管理
 */
@RestController
@RequestMapping("/job")
@Api(tags = "岗位管理")
@RequiredArgsConstructor
public class AdminJobController {

    private final AdminJobService adminJobService;

    private static final String ENTITY_NAME = "job";

    @Log("新增岗位")
    @ApiOperation("新增岗位")
    @PostMapping
    @SaCheckPermission("job.add")
    public EzResult<Object> createJob(@Validated @RequestBody AdminJobVo adminJobVo){
        if (adminJobVo.getId() != null) {
            throw new BadRequestException("A new "+ ENTITY_NAME +" cannot already have an ID");
        }
        adminJobService.createJob(adminJobVo);
        return EzResult.ok();
    }

    @Log("删除岗位")
    @ApiOperation("删除岗位")
    @DeleteMapping
    @SaCheckPermission("job.delete")
    public EzResult<Object> deleteJob(@RequestBody List<Long> ids){
        adminJobService.deleteJob(ids);
        return EzResult.ok();
    }

    @Log("修改岗位")
    @ApiOperation("修改岗位")
    @PutMapping
    @SaCheckPermission("job.update")
    public EzResult<Object> updateJob(@Validated @RequestBody AdminJobVo adminJobVo){
        adminJobService.updateJob(adminJobVo);
        return EzResult.ok();
    }

    @ApiOperation("查询岗位")
    @GetMapping
    public EzResult<Object> queryRole(@RequestParam(required = false) Long id) {
        return EzResult.ok(adminJobService.findJobById(id));
    }

    @ApiOperation("按条件分页查询岗位")
    @PostMapping("/page")
    public EzResult<Object> pageQueryJob(@RequestBody BaseSearchBody<AdminJob> baseSearchBody){
        return EzResult.ok(adminJobService.pageQueryJob(baseSearchBody.getSearchCondition(),baseSearchBody.getPage()));
    }

    @ApiOperation("导出岗位数据")
    @GetMapping(value = "/download")
    public void exportJob(HttpServletResponse response, List<AdminJobVo> adminJobVo) throws IOException {
        adminJobService.exportJob(response,adminJobVo);
    }
}
